#include<iostream>
#include<cstdlib>
#include<string>
#include<cstring>
#include<fstream>
#include<vector>
#include<cctype>
#include<algorithm>
#include<sstream>
#include<random>
#include<unordered_set>
using namespace std;
enum parts
{
    Illegal=0, VoltS, CurrS, Reg, Capc, Lth, nLoop, nShort, bOpen
};
string trimLeft(const string& str) {
    size_t start = 0;
    // ?????????????????????
    while (start < str.length() && isspace(str[start])) {
        ++start;
    }
    return str.substr(start);
}
vector<string> split(const string& str) {
    vector<string> result;
    string temp;
    istringstream stream(str);

    while (stream >> temp) {
        result.push_back(temp);
    }

    return result;
}
int check_legal(vector<string> subs)
{
    bool check=false;
    int cate=0;
    int num=0;
    for(int i=0; i<subs.size(); i++)
    {
        int pos = subs[i].find("V");
        if(pos!=subs[i].npos)
        {
            cate=1;
            return cate;
        }
        pos = subs[i].find("I");
        if(pos!=subs[i].npos)
        {
            cate=2;
            return cate;
        }
        pos = subs[i].find("R");
        if(pos!=subs[i].npos)
        {
            cate=3;
            return cate;
        }
        pos = subs[i].find("C");
        if(pos!=subs[i].npos)
        {
            cate=4;
            return cate;
        }
        pos = subs[i].find("L");
        if(pos!=subs[i].npos)
        {
            cate=5;
            return cate;
        }
    }
    return Illegal;
}
int main()
{
	ifstream infile;
	ofstream outfile;
    infile.open("netlistDump.sp");
    outfile.open("ComponentCalculate.txt");
    int nI, nV, nC, nR, nL, nShort, nLoop, circuit;
    bool bOpen;
    nI = 0; nV = 0; nC = 0; nR = 0; nL = 0; nShort = 0; nLoop = 0; circuit = 0;
    bOpen = false;
    string buf;
    buf.clear();
    vector<string> subs;
    buf.clear();
    while(getline(infile, buf))
    {
        buf = trimLeft(buf);
        if(!buf.empty())
        {
            subs.clear();
            subs = split(buf);
            int recognise_part = check_legal(subs);
            if(recognise_part>0)
            {
                if(recognise_part==1) nV++;
                if(recognise_part==2) nI++;
                if(recognise_part==3) nR++;
                if(recognise_part==4) nC++;
                if(recognise_part==5) nL++;

            }
        }
        else
        {
        	circuit++;
		}
        buf.clear();
    }
    outfile<<"nVoltS\t"<<nV<<endl
    <<"nCurrS\t"<<nI<<endl
    <<"nR\t"<<nR<<endl
    <<"nC\t"<<nC<<endl
    <<"nL\t"<<nL<<endl
    <<"nCircuit\t"<<circuit<<endl;
    infile.close();
    outfile.close();
}
